var session = require('express-session');
var cookieParser = require('cookie-parser');
var sd = require('silly-datetime');
var Warp = require('../database/config.js');
var crypto = require('crypto');
const nodemailer = require('nodemailer');
var mailer = require('./mail.js');

var ctrl = {};

ctrl.userLogin = function(req, res, next){
	var username = req.body.username;
	var password = req.body.password;
	var user = {
		username: username,
		password: password
	}
	Warp.warp.query('select * from ? where password = ?',[username,password],function(err , results){
	if(results !== " ")
	{
		res.cookie("user", {username: username}, {maxAge: 600000});
		res.redirect('index');
		var logintime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
		Warp.warp.query('update taneiuser set LastLogin = ? where password = ? and username = ?', 
			[logintime , password , username]);
	}
	else
	{
		req.error = '用户名密码错误';
		res.render('login', req);
	}})
	//next();
}

ctrl.setIndex = function(req, res, next)
{
	if(req.cookies.user !== null)
	{
		req.user = req.cookies.user;
	}
	res.render('index', req);
}

ctrl.delCookie = function(req, res, next)
{
	res.clearCookie('user');
	res.redirect('index');
}

ctrl.active = function(req, res, next){                 
// 发送激活邮件
    mailer.send({
        to: req.body.email,
        subject: '欢迎注册tnpc',
        html: '您的验证码为' + req.body.secret
    });     
};

//注册请求

ctrl.signup = function(req , res , next)
{
	var time = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
	var username = res.body.username;
	var password = res.body.password;
	var userID = res.body.userID;
	var college = res.body.college;
	var wechatID = res.body.wechatID;
	var name = res.body.name;
	var email = res.body.mail;
	var userLevel = 1;
	var tel = tel;
	var random = res.body.num;
	if(random == res.body.secret)
	{
		Warp.warp.query("insert into taneiuser values(?,?,?,?,?,?,?,?,?,?,?,?)",
			[userID,password,college,username,wechatID,name,email,userLevel,time,time,time],function(err,results)
			{
				if(err) throw err;
				else console.log(JSON.stringify(results));
			})
		res.render('/Issignup');
	}
	else
		res.render("验证码错误");
}

ctrl.changeLevel = function(req , res , next)
{
	var changeTime = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
	Warp.warp.query("update taneiuser set userLevel = ? where username = ? and password = ?", 
		[req.body.level , req.body.username , req.body.password],function(err)
		{
			if(err) res.render("修改权限失败");
			else res.render("修改权限成功");
		});
	Warp.warp.query("update taneiuser set ChangeLevTime = ? where username = ? and password = ?" ,
		 [changeTime , req.body.username , req.body.password]);
}

ctrl.changePass = function(req , res , next)
{
	var randomnum = ;//随机数
	if(randomnum == req.body.num)
	{
		Warp.warp.query("update taneiuser set password = ? where email = ?", [req.body.password , 
			req.body.mail],function(err)
			{
				if(err) res.render("修改密码失败");
				else res.render("修改密码成功");
			})
	}
	else
		res.render("验证码错误");
}


module.exports = ctrl;